<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="_meta :: header">
    <title>编辑</title>
</head>
<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form form form-horizontal" id="form-draft-edit" action="#"
              th:action="@{/draft/writer/edit}" th:object="${draft}" enctype="multipart/form-data">
            <input type="hidden" name="id" th:value="${draft.id}"/>
            <input type="hidden" id="action" name="action" value="">

            <div class="layui-form-item">
                <label class="layui-form-label"><span
                            class="c-red">*</span>稿件标题：</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input" th:value="${draft.draftTitle}" name="draftTitle"  placeholder="修改稿件标题" id="draftTitle"/>
                    </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span
                        class="c-red">*</span>文件名：</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" th:value="${draft.fileName}" name="fileName"  placeholder="修改文件名" id="fileName"/>
                </div>
            </div>


            <div class="layui-form-item">
                <label class="layui-form-label"><span
                            class="c-red">*</span>稿件内容：</label>
                <div class="layui-input-block">
                    <script id="editor" th:text="${draft.draftText}" name="draftText" autofocus type="text/plain" style="height: 500px">
                    </script>
                    <input id="editorInput" type="hidden" name="" th:value="${draft.draftText}">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"><span
                            class="c-red">*</span>稿件类型：</label>
                    <div class="layui-input-inline">
                        <div th:if="${roleId == 6}">
                            <input name="draftType" th:checked="${draft.draftType eq c.key}" type="radio" th:if="${c.key < 5}"
                                   th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}"/>
                            <!--<input type="text" class="layui-input" th:value="${draft.draftType}" name="draftType"  placeholder="修改稿件类型" id="draftType"/>-->
                        </div>
                        <div th:if="${roleId == 40}">
                        <input name="draftType" th:checked="${draft.draftType eq c.key}" type="radio" th:if="${c.key == 5 }"
                               th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}"/><br>
                        <input name="draftType" th:checked="${draft.draftType eq c.key}" type="radio" th:if="${c.key == 6 }"
                                   th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}"/><br>
                        <!--<input type="text" class="layui-input" th:value="${draft.draftType}" name="draftType"  placeholder="修改稿件类型" id="draftType"/>-->
                    </div>
                        <div th:if="${roleId == 46}">
                            <input name="draftType" th:checked="${draft.draftType eq c.key}" type="radio" th:if="${c.key >= 7}"
                                   th:each="c:${draftTypes}"  th:value="${c.key}" th:text="${c.value}"/><br>
                            <!--<input type="text" class="layui-input" th:value="${draft.draftType}" name="draftType"  placeholder="修改稿件类型" id="draftType"/>-->
                        </div>

                    </div>

            </div>
            <div class="layui-form-item" th:if="${reason ne null}">
                <label class="layui-form-label">驳回理由：</label>
                <div class="layui-input-block">
                    <textarea  readonly type="text" rows="10" th:text="${reason}" cols="100" /></textarea>
                </div>
            </div>
            <input type="hidden" name="draftNum"  placeholder="修改稿件字数" id="draftNum"/>
            <div class="layui-form-item">
                <label class="layui-form-label">
                </label>
                <button  class="layui-btn" id="draftBox" type="submit" onclick="submitFun('save');">
                    保存
                </button>
                <button  class="layui-btn" id="subbtn" type="submit" onclick="submitFun('submit');">
                    发布
                </button>
            </div>
        </form>
    </div>
</div>
<!--_footer 作为公共模版分离出去-->
<div th:replace="_footer :: footerjs"></div>
<div th:replace="_ueditor :: ueditor"></div>
<!--/_footer 作为公共模版分离出去-->
<!--请在下方写此页面业务相关的脚本-->
<script>
    layui.use(['form', 'layer','laydate'],function() {
        $ = layui.jquery;
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer;
        lay('.timeSpace').each(function(){
            laydate.render({
                elem: this
                ,trigger: 'click'
                ,type: 'datetime'
            });
        });
    });

    function submitFun(act)
    {
        $('#action').val(act);
    };
    $(function () {
        $("#form-draft-edit").validate({
            rules: {//自定义规则，key:value 的形式，key 是要验证的元素，value 可以是字符串或对象。
                draftTitle:{
                    required: true,
                },
                draftText:{
                    required: true,
                },
                 draftNum:{
                    required: true,
                },
                draftType:{
                    required: true,
                },
                fileName:{
                    required: true,
                },
            },
            onkeyup:false,//类型 Boolean，默认 true，指定是否在敲击键盘时验证。
            debug : true,//进行调试模式（表单不提交）。
            success : "valid",//要验证的元素通过验证后的动作，如果跟一个字符串，会当作一个 css 类，也可跟一个函数。
            submitHandler : function (form) {//通过验证后运行的函数，里面要加上表单提交的函数($(form).ajaxSubmit();)，否则表单不会提交。
//            var data = UE.getEditor('editor').getContent();
//            $("#editorInput").val(data);
            var draftNum = UE.getEditor('editor').getContentTxt().length;
            $("#draftNum").val(draftNum);
            $.lock();
            if($('#action').val() == 'submit'){
                $(form).ajaxSubmit({//使用 ajax 方式提交
                    type: 'POST',
                    url: "/draft/writer/edit",
                    success: function (data) {
                        if (data.code == "1") {
                            selfmsg('发布成功!', 1, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                                // window.location.href = "/draft/writer?statusCode=1001";
                            });
                        }else {
                            selfmsg('发布失败!');
                            $.unlock();
                        }
                    },
                    error: function () {
                        selfmsg('发布异常!', 5);
                        $.unlock();
                    }
                });
            }else if ($('#action').val() == 'save'){
                $(form).ajaxSubmit({//使用 ajax 方式提交
                    type: 'POST',
                    url: "/draft/writer/edit",
                    success: function (data) {
                        if (data.code == "1") {
                            selfmsg('保存成功!', 1, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.location.reload();
                                parent.layer.close(index);
                                // window.location.href = "http://114.55.208.200/draft/writer/draftBox";
                            });
                        }else {
                            selfmsg('保存失败!');
                            $.unlock();
                        }
                    },
                    error: function () {
                        selfmsg('保存异常!', 5);
                        $.unlock();
                    }
                });
            }



        }
    });
    });

    function showTxt()
    {
        var preHtml = UE.getEditor('editor').getContent(); <!-- execCommand( 'preview' );-->
        layer.open({
            title: '稿件预览',
            content: preHtml,
            btn: ['确定', '取消'],
            yes: function(index, layero){//两个参数:当前层索引,当前层DOM对象
                //按钮【确认】的回调
            },
            btn2: function(index, layero){
                //按钮【取消】的回调
                //return false 开启该代码可禁止点击该按钮关闭
            },
        });
    };
</script>
<!--/请在上方写此页面业务相关的脚本-->
</body>
</html>